package com.forever.thread.concurrent;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class ConcurrentHashMapTest {

	public static void main(String[] args) {
		ConcurrentMap<String, String> map = new ConcurrentHashMap<String, String>();
		System.out.println(map.size());
		System.out.println(map.putIfAbsent("1", "value"));
		System.out.println(map.size());
		System.out.println(map.putIfAbsent("1", "value"));

		System.out.println(ConcurrentHashMapTest.getInstance());
		System.out.println(ConcurrentHashMapTest.getInstance());
		System.out.println(ConcurrentHashMapTest.getInstance());
	}

	private static final ConcurrentMap<String, ConcurrentHashMapTest> map = new ConcurrentHashMap<String, ConcurrentHashMapTest>();
	private static ConcurrentHashMapTest instance;
	private static final String INSTANCE = "INSTANCE";

	/**
	 * 线程安全的单例模式
	 * 
	 * @return
	 */
	public static ConcurrentHashMapTest getInstance() {
		if (instance == null) {
			map.putIfAbsent(INSTANCE, new ConcurrentHashMapTest());
			instance = map.get(INSTANCE);
		}
		return instance;
	}

	private ConcurrentHashMapTest() {
	}
}
